using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using FAQComponents;
using FAQComponents.Objects;
using FAQComponents.API;
using Adge.UI.WebControls;

namespace Adge.Web.Faq.Admin
{
	/// <summary>
	/// Summary description for CreateFAQ.
	/// </summary>
	public partial class CreateFAQ : ClientSidePage
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			MonitorChanges(question);
			MonitorChanges(category);
			MonitorChanges(answer);

			BypassModifiedMethod(btnCreateFAQ);

			if (!Page.IsPostBack)
			{
				// Load up the applicable categories for this user
				User u = FAQContext.GetContext().LoggedOnUser;

				if (u.Permissions.Count == 0 && !u.IsAdmin)
					throw new ApplicationException("You do not have permission to add a FAQ to any category in the system.");
				else
					category.DataSource = u.Permissions;

				category.DataBind();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void btnCancel_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("FAQAdmin.aspx");
		}

		protected void btnCreateFAQ_Click(object sender, System.EventArgs e)
		{
			if (!Page.IsValid) return;

			User u = FAQContext.GetContext().LoggedOnUser;

			FAQ f = new FAQ();
			f.Question = question.Text;
			f.CategoryID = InvariantString.ToInt32(category.SelectedValue);
			f.CreatedByUserID = u.UserID;
			if (u.IsAdmin)
				f.Approved = true;
			else
			{
				Category p = u.Permissions.FindCategory(f.CategoryID);
				f.Approved = p.UsersRole != Roles.Contributor;	// approve unless Contributor
			}

			FAQAPI.AddFAQ(f, answer.Text);

			Response.Redirect("FAQAdmin.aspx");
		}
	}
}
